<! vim+xdebug: http://localhost:8080/demo/index.php?XDEBUG_SESSION_START=1 >

<html>
<meta http-equiv="content-type" content="text/html; charset=gb2312">  
     <head>
     <link rel="stylesheet" href="style.css">  
     <script type="text/javascript">
function checkBG() 
{
    var url = document.getElementById('weather_bg').value;
    var img = new Image();
    var valid = true;
    img.onerror = 
    function() 
    { 
        alert(url + "is not an image!");
        valid = false; 
    }
    img.src = url; 
    return valid;
}
 
var weather_img = new Array();
var weather_img_idx = 0;

function hndlr(response) 
{
    for (var i = 0; i < response.items.length; i++) 
    {
        var item = response.items[i];
        weather_img.push(item.link);
    }
}

function nextBG(forward=true) 
{
    if (weather_img_idx < weather_img.length) 
    {
        document.body.background = weather_img[weather_img_idx];
        document.getElementById('weather_bg').value = 
        weather_img[weather_img_idx];
        delta = forward? 1: -1;
        weather_img_idx = (weather_img_idx+delta) % weather_img.length;
        if (weather_img_idx < 0) 
        {
            weather_img_idx = weather_img.length-1;
        }
    }
} 
</script>
</head>

<?php 
include 'pinyin.php'; 
include 'background.php'; 
//include 'ascii.php';

$html=file_get_contents('http://iframe.ip138.com/ic.asp');
$P=mb_strpos($html,"省");
$C=mb_strpos($html,"市");
$city=mb_substr($html,$P+2,$C-$P-2);
//$city = Pinyin($city);
$city = "haikou";
$weather_key = "f82255194732a2f0";
$weather_api = "http://api.wunderground.com/api/${weather_key}/conditions/q/CN/${city}.json";
$json_string = file_get_contents($weather_api);
$parsed_json = json_decode($json_string); 
$location = $parsed_json->{'current_observation'}->{'display_location'}->{'city'}; 
$temp_c = $parsed_json->{'current_observation'}->{'temp_c'}; 
$weather = $parsed_json->{'current_observation'}->{'weather'}; 
$icon_url = $parsed_json->{'current_observation'}->{'icon_url'}; 
list($bg, $found) = get_background($weather);
?>

<body background="<?php echo $bg ?>" >
    <h1>My Desktop</h1>
<?php
$google_api_key = "AIzaSyAqoFIUg2rhSBvwk036ZzNI_0G706q0_cw";
$google_search_key = "002774109548475617487:legunzgwq2w";
$google_search_terms = $weather." weather";
echo "Current temperature in ${location} is: ${temp_c}C. <br>"; 
echo "<img src=${icon_url} /><br>";
if (!$found) 
{
    if ($status['status'] == 'error') 
    {
        echo $status['message'];
    }
        
    echo <<<EOT
<form action="./" method="POST">
Background for "$weather": 
<input name="weather_bg" id="weather_bg" type="text" />
<input name="weather" value="$weather" type="hidden" />
<input value="Save" type="submit" onclick="checkBG();" />
<input value="Next" type="button" onclick="nextBG();" />
<input value="Prev" type="button" onclick="nextBG(false);" />
</form>
<div id="content"></div>
<script src="https://www.googleapis.com/customsearch/v1?key=$google_api_key&cx=$google_search_key&q=$google_search_terms&callback=hndlr&searchType=image">
</script>
EOT;
}
?>
</body>
</html>
